package com.api.dengcj.epl.formmode;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dengcj.ecology.mode.ModeUtil;
import com.dengcj.entity.ApiResult;
import com.dengcj.util.CjApiUtil;
import com.engine.cube.biz.CodeBuilder;
import weaver.formmode.setup.ModeRightInfo;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 对建模新增数据进行必要重构, 包含添加权限, 生成编号等
 * @Auther: dengchunjie
 * @Date: 2023/2/1 10:36
 */

@Path("/epl/formmode/datarefactor")
public class DataRefactor {

    /**
     * 新增数据时添加建模默认共享和生成建模配置的编号字段, 用于处理单条数据
     * @param jsonObject
     * {
     *     "creator": 1,
     *     "modeid": 23,
     *     billid: 222
     * }
     *
     * @return
     */
    @Path("/newdata")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String newData(JSONObject jsonObject) {
        ApiResult<String> apiResult = new ApiResult();

        // 装入需要必填验证的字段
        List<String> notNullKeys = new ArrayList<>();
        notNullKeys.add("creator");
        notNullKeys.add("modeid");
        notNullKeys.add("billid");

        String validateResult = CjApiUtil.jsonEmptyKeyValidate(jsonObject, notNullKeys);
        if(StrUtil.isBlank(validateResult)){
            return apiResult.error(validateResult);
        }

        // 创建人Id
        int creator = jsonObject.getInt("creator");
        // 模块Id
        int modeId = jsonObject.getInt("modeid");

        ModeUtil modeUtil = new ModeUtil();
        // 表单ID
        int formId = modeUtil.getFormidByModeid(modeId);
        int billId = jsonObject.getInt("billid");

        // 是否添加文档权限 默认false
        Boolean addDocShare = jsonObject.getBool("adddocshare");
        if(addDocShare == null){
            addDocShare = false;
        }

        ModeRightInfo modeRightInfo = new ModeRightInfo();
        execNew(modeRightInfo, creator, modeId, formId, billId, addDocShare);

        return apiResult.success();
    }

    /**
     * 新增数据时添加建模默认共享和生成建模配置的编号字段, 可处理多条数据
     * @param jsonObject
     * {
     *     "creator": 1,
     *     "modeid": 23,
     *     billid: [222,223,224,225]
     * }
     * @return
     */
    @Path("/newdatas")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String newDatas(JSONObject jsonObject) {
        ApiResult<String> apiResult = new ApiResult();

        // 装入需要必填验证的字段
        List<String> notNullKeys = new ArrayList<>();
        notNullKeys.add("creator");
        notNullKeys.add("modeid");
        notNullKeys.add("billid");

        String validateResult = CjApiUtil.jsonEmptyKeyValidate(jsonObject, notNullKeys);
        if(StrUtil.isBlank(validateResult)){
            return apiResult.error(validateResult);
        }

        // 创建人Id
        int creator = jsonObject.getInt("creator");
        // 模块Id
        int modeId = jsonObject.getInt("modeid");

        ModeUtil modeUtil = new ModeUtil();
        // 表单ID
        int formId = modeUtil.getFormidByModeid(modeId);
        JSONArray billIdArray = jsonObject.getJSONArray("billid");

        int arrLen = billIdArray.size();

        // 是否添加文档权限 默认false
        Boolean addDocShare = jsonObject.getBool("adddocshare");
        if(addDocShare == null){
            addDocShare = false;
        }

        ModeRightInfo modeRightInfo = new ModeRightInfo();
        for (int i = 0; i < arrLen; i++) {
            int billId = billIdArray.getInt(i);
            execNew(modeRightInfo, creator, modeId, formId, billId, addDocShare);
        }

        return apiResult.success();
    }

    @Path("/modifydata")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String modifyData(JSONObject jsonObject) {
        ApiResult<String> apiResult = new ApiResult();

        // 装入需要必填验证的字段
        List<String> notNullKeys = new ArrayList<>();
        notNullKeys.add("creator");
        notNullKeys.add("modeid");
        notNullKeys.add("billid");

        String validateResult = CjApiUtil.jsonEmptyKeyValidate(jsonObject, notNullKeys);
        if(StrUtil.isBlank(validateResult)){
            return apiResult.error(validateResult);
        }

        // 创建人Id
        int creator = jsonObject.getInt("creator");
        // 模块Id
        int modeId = jsonObject.getInt("modeid");

        int billId = jsonObject.getInt("billid");

        // 是否添加文档权限 默认false
        Boolean addDocShare = jsonObject.getBool("adddocshare");
        if(addDocShare == null){
            addDocShare = false;
        }

        ModeRightInfo modeRightInfo = new ModeRightInfo();
        execModify(modeRightInfo, creator, modeId, billId, addDocShare);

        return apiResult.success();
    }

    @Path("/modifydatas")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String modifyDatas(JSONObject jsonObject) {
        ApiResult<String> apiResult = new ApiResult();

        // 装入需要必填验证的字段
        List<String> notNullKeys = new ArrayList<>();
        notNullKeys.add("creator");
        notNullKeys.add("modeid");
        notNullKeys.add("billid");

        String validateResult = CjApiUtil.jsonEmptyKeyValidate(jsonObject, notNullKeys);
        if(StrUtil.isBlank(validateResult)){
            return apiResult.error(validateResult);
        }

        // 创建人Id
        int creator = jsonObject.getInt("creator");
        // 模块Id
        int modeId = jsonObject.getInt("modeid");

        JSONArray billIdArray = jsonObject.getJSONArray("billid");

        int arrLen = billIdArray.size();

        // 是否添加文档权限 默认false
        Boolean addDocShare = jsonObject.getBool("adddocshare");
        if(addDocShare == null){
            addDocShare = false;
        }

        ModeRightInfo modeRightInfo = new ModeRightInfo();
        for (int i = 0; i < arrLen; i++) {
            int billId = billIdArray.getInt(i);
            execModify(modeRightInfo, creator, modeId, billId, addDocShare);
        }

        return apiResult.success();
    }


    public void execNew(ModeRightInfo modeRightInfo, int creatorId, int modeId, int formId, int billId, boolean addDocShare){
        // 先添加建模的默认共享
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(creatorId,modeId,billId);

        // 生成编号
        CodeBuilder cbuild = new CodeBuilder(modeId);
        cbuild.getModeCodeStr(formId, billId);//生成编号

        // 附件字段添加权限
        if(addDocShare){
            modeRightInfo.addDocShare(creatorId,modeId,billId);//给文附件字段加权限
        }
    }

    public void execModify(ModeRightInfo modeRightInfo, int creatorId, int modeId, int billId, boolean addDocShare){
        // 先添加建模的默认共享
        modeRightInfo.rebuildModeDataShareByEdit(creatorId,modeId,billId);//编辑时重新生成默认共享

        // 附件字段添加权限
        if(addDocShare){
            modeRightInfo.addDocShare(creatorId,modeId,billId);//给文附件字段加权限
        }
    }
}
